package com.zHan.system.controller.user;

import com.zHan.common.result.Result;
import com.zHan.common.utils.ThreadLocalUtil;
import com.zHan.model.dto.UpdatePwdDto;
import com.zHan.model.system.SysUser;
import com.zHan.model.vo.UserDetailsVo;
import com.zHan.system.anno.Log;
import com.zHan.system.exception.MyException;
import com.zHan.system.menu.BusinessType;
import com.zHan.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.regex.Pattern;

/**
 * ClassName: UserController
 * Description:
 * date: 2023/12/27 0027 17:05
 *
 * @author zHan
 */
@Api(tags = "个人中心管理接口")
@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private ISysUserService userService;

    /**
     * 更新用户头像
     *
     * @param avatarUrl
     * @return
     */
    @PutMapping("/updateAvatar")
    @ApiOperation(value = "更新用户头像")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    public Result updateAvatar(@RequestParam String avatarUrl) {
        String urlPattern = "(ht|f)tp(s?)://[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:" +
                "(0-9)*)*(/?)([a-zA-Z0-9\\-.?,'/\\\\&%+$#_=]*)?";
        if (!Pattern.matches(urlPattern, avatarUrl)) {
            throw new MyException(201, "url地址不正确");
        }
        boolean isSuccess = userService.updateAvatar(avatarUrl);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 重置密码
     *
     * @param updatePwdDto
     * @return
     */
    @PutMapping("/updatePwd")
    @ApiOperation(value = "重置密码")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    public Result updatePwd(@RequestBody UpdatePwdDto updatePwdDto,
                            @RequestHeader("token") String token) {
        boolean isSuccess = userService.updatePwd(updatePwdDto, token);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 获取用户详情
     *
     * @return
     */
    @GetMapping("/details")
    @ApiOperation(value = "获取用户详情")
    public Result details() {
        // 获取username
        Map<String, String> map = ThreadLocalUtil.get();
        String username = map.get("username");
        // 获取用户详情
        UserDetailsVo userDetailsVo = userService.getUserDetails(username);
        if (userDetailsVo != null) {
            return Result.ok(userDetailsVo);
        }
        return Result.fail();
    }

    /**
     * 更新用户信息
     *
     * @param sysUser
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新用户信息")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    public Result updateAvatar(@RequestBody SysUser sysUser) {
        Map<String, String> map = ThreadLocalUtil.get();
        // 获得用户的id
        String userId = map.get("userId");
        if (userId == null) {
            throw new MyException(20001, "用户不存在");
        }
        // 赋值用户id
        sysUser.setId(userId);
        // 参数校验
        checkArgs(sysUser);
        // 更新用户信息
        boolean isSuccess = userService.updateById(sysUser);
        if (isSuccess) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 更新用户信息的参数校验
     *
     * @param sysUser
     */
    private void checkArgs(SysUser sysUser) {
        // 姓名和手机号不能为空
        String phone = sysUser.getPhone();
        String name = sysUser.getName();
        if (StringUtils.isEmpty(name)) {
            throw new MyException(201, "请输入姓名");
        }
        if (StringUtils.isEmpty(phone)) {
            throw new MyException(201, "请输入手机号");
        }
        // 判断手机号是否正确
        String phonePattern = "^1[3|4|5|7|8][0-9]{9}$";
        // 手机号判断
        if (!Pattern.matches(phonePattern, phone)) {
            throw new MyException(201, "手机号格式不正确");
        }
    }

}
